package com.example.longimageView;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.longimageView.bean.UserBean;
import com.example.longimageView.db.UserDataBaseBean;
import com.example.longimageView.db.UserDbManager;
import com.example.longimageView.utils.FireGsonUtil;

import org.xutils.DbManager;
import org.xutils.db.Selector;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * @author: njb
 * @date: 2023/5/10 23:12
 * @desc:
 */
public class DBTestActivity extends AppCompatActivity {
    private static final String TAG = DBTestActivity.class.getName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db_test);
    }

    //增加
    public void add(View v) {
        UserDataBaseBean userDataBaseBean = new UserDataBaseBean("1","张三","test@qq.com","12","男","3","30","6","food","b","20:0a:0d:e8:86:44");
   /*     userDataBaseBean.id = "1";
        userDataBaseBean.name = "张三";
        userDataBaseBean.email = "test@qq.com";
        userDataBaseBean.sex = "男";*/
        try {
            UserDbManager.getInstance().saveDevice(userDataBaseBean);
            ToastUtils.showShort(TAG,"---save success---");
            LogUtils.d("tag", FireGsonUtil.objectToJson(userDataBaseBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询全部
    public void query_all(View v) {
        try {
            List<UserDataBaseBean> list = UserDbManager.getInstance().getAllDevices();
            for (UserDataBaseBean u : list) {
                LogUtils.i(TAG, String.valueOf(u));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //条件查找
    public void query(View view) {
        try {
            UserDataBaseBean userBean = UserDbManager.getInstance().queryDeviceByName("张三");
            ToastUtils.showShort(TAG,userBean.toString());
            LogUtils.i("tag", String.valueOf(userBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改
    public void update(View view) {
        UserBean userBean = new UserBean();
        userBean.setId("2");
        userBean.setEmail("test@qq.com");
        userBean.setName("李四");
        userBean.setAge("28");
        userBean.setSex("女");
        UserDataBaseBean userDataBaseBean = new UserDataBaseBean();
        userDataBaseBean.id = userBean.getId();
        userDataBaseBean.name = userBean.getName();
        userDataBaseBean.email = userBean.getEmail();
        userDataBaseBean.age = userBean.getAge();
        userDataBaseBean.country = "2";
        try {
            UserDbManager.getInstance().updateDevice(userDataBaseBean);
            ToastUtils.showShort("更新成功");
            LogUtils.i(TAG, "更新成功" + FireGsonUtil.objectToJson(userDataBaseBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    ///删除
    public void delete(View view) {
        try {
            UserDbManager.getInstance().deleteAllDevices();
            ToastUtils.showShort("删除成功");
            LogUtils.i(TAG, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
